package com.cskaoyan.service.admin;

import com.cskaoyan.bean.MarketAdmin;
import com.cskaoyan.bean.MarketAdminExample;
import com.cskaoyan.bean.MarketLog;
import com.cskaoyan.bean.MarketLogExample;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.cskaoyan.mapper.MarketLogMapper;
import com.cskaoyan.model.admin.bo.AdminCreateBo;
import com.cskaoyan.model.admin.bo.BaseParam;
import com.cskaoyan.model.admin.vo.AdminCreateVo;
import com.cskaoyan.model.admin.vo.SystemAdminInfoVo;
import com.cskaoyan.model.admin.vo.SystemAdminListVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author hanshuai
 * @version 1.0
 * @description @TODO
 * @date 2021/11/29 18:52
 */
@Service
public class SystemAdminServiceImpl implements SystemAdminService {
    @Autowired
    MarketAdminMapper marketAdminMapper;

    @Autowired
    MarketLogMapper marketLogMapper;

    @Override
    public SystemAdminListVo getList(String username, BaseParam baseParam) {
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        MarketAdminExample example = new MarketAdminExample();
        SystemAdminListVo systemAdminListVo = new SystemAdminListVo();
        List<MarketAdmin> marketAdmins;
        MarketAdminExample.Criteria criteria = example.createCriteria();
        if (username != null) {
            criteria.andUsernameLike("%" + username + "%");
        }
        criteria.andDeletedEqualTo(false);
        example.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());

        marketAdmins = marketAdminMapper.selectByExample(example);
        List<SystemAdminInfoVo> systemAdminInfoVos = new ArrayList<>();
        for (MarketAdmin marketAdmin : marketAdmins) {
            SystemAdminInfoVo systemAdminInfoVo = new SystemAdminInfoVo();
            systemAdminInfoVo.setAvatar(marketAdmin.getAvatar());
            systemAdminInfoVo.setId(marketAdmin.getId());
            systemAdminInfoVo.setUsername(marketAdmin.getUsername());
            String roleId = marketAdmin.getRoleIds();
            String replace1 = roleId.replace("[", "");
            String strNum = replace1.replace("]", "");
            String[] split = strNum.split(",");
            List<Integer> roleIds = new ArrayList<>();
            for (String s : split) {
                System.out.println(s);
                String trim = s.trim();
                roleIds.add(Integer.parseInt(trim));
            }

            systemAdminInfoVo.setRoleIds(roleIds);
            systemAdminInfoVos.add(systemAdminInfoVo);
        }
        systemAdminListVo.setLimit(baseParam.getLimit());
        systemAdminListVo.setPage(baseParam.getPage());
        systemAdminListVo.setList(systemAdminInfoVos);

        PageInfo pageInfo = new PageInfo(marketAdmins);
        systemAdminListVo.setPages(pageInfo.getPages());
        systemAdminListVo.setTotal((int) pageInfo.getTotal());
        return systemAdminListVo;
    }

    @Override
    public AdminCreateVo createAdmin(AdminCreateBo adminCreateBo) {
        MarketAdmin marketAdmin = new MarketAdmin();
        Date date = new Date();
        marketAdmin.setId(null);
        marketAdmin.setAvatar(adminCreateBo.getAvatar());
        marketAdmin.setUsername(adminCreateBo.getUsername());
        String roleId = adminCreateBo.getRoleIds().toString();
        marketAdmin.setRoleIds(roleId);
        marketAdmin.setAddTime(date);
        marketAdmin.setUpdateTime(date);
        marketAdmin.setPassword(adminCreateBo.getPassword());

        marketAdminMapper.insertSelective(marketAdmin);
        MarketAdminExample example = new MarketAdminExample();
        example.createCriteria().andUsernameEqualTo(adminCreateBo.getUsername()).andDeletedEqualTo(false);
        List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(example);
        Integer id = marketAdmins.get(0).getId();
        AdminCreateVo adminCreateVo = new AdminCreateVo();

        adminCreateVo.setId(id);
        adminCreateVo.setAddTime(date);
        adminCreateVo.setPassword(adminCreateBo.getPassword());
        adminCreateVo.setAvatar(adminCreateBo.getAvatar());
        adminCreateVo.setRoleIds(adminCreateBo.getRoleIds());
        adminCreateVo.setUpdateTime(date);
        adminCreateVo.setUsername(adminCreateBo.getUsername());
        return adminCreateVo;
    }

    @Override
    public AdminCreateVo updateAdmin(AdminCreateBo adminCreateBo) {
        MarketAdmin marketAdmin = new MarketAdmin();
        marketAdmin.setUsername(adminCreateBo.getUsername());
        marketAdmin.setUpdateTime(new Date());
        marketAdmin.setAvatar(adminCreateBo.getAvatar());
        marketAdmin.setPassword(adminCreateBo.getPassword());
        marketAdmin.setRoleIds(adminCreateBo.getRoleIds().toString());
        MarketAdminExample example = new MarketAdminExample();
        example.createCriteria().andIdEqualTo(adminCreateBo.getId());
        marketAdminMapper.updateByExampleSelective(marketAdmin, example);
        return new AdminCreateVo(null, adminCreateBo.getAvatar(), adminCreateBo.getId(), adminCreateBo.getPassword(), adminCreateBo.getRoleIds(), marketAdmin.getUpdateTime(), adminCreateBo.getUsername());
    }

    @Override
    public void deleteAdmin(AdminCreateBo adminCreateBo, HttpSession session) {
        marketAdminMapper.deleteByPrimaryKey(adminCreateBo.getId());
        MarketLogExample example = new MarketLogExample();
        MarketLog marketLog = new MarketLog();
        marketLog.setDeleted(false);
        Subject subject = SecurityUtils.getSubject();
        marketLog.setAdmin(adminCreateBo.getUsername());
        marketLog.setIp(subject.getSession().getHost());
        marketLog.setAction("删除管理员");
        marketLog.setStatus(true);
        marketLog.setResult("成功");
        marketLog.setUpdateTime(new Date());
        marketLogMapper.insertSelective(marketLog);
    }


}
